package com.powroznik.tracker.engine.services;

import java.util.Collection;

import com.powroznik.tracker.engine.data.Account;
import com.powroznik.tracker.engine.data.Device;
import com.powroznik.tracker.engine.data.Location;
import com.powroznik.tracker.engine.data.TrackerEngineDao;
import com.powroznik.tracker.engine.data.User;

public class ValidationService {
	private TrackerEngineDao trackerEngineDao;

	public boolean isGenuine(Object o) {
		return isGenuine(o, null);
	}

	public boolean isGenuine(Object o, Boolean n) {
		if (o instanceof String) {
			return true;
		} else if (o instanceof Collection) {
			Collection c = (Collection) o;
			for (Object p : c) {
				if (!isGenuine(p, n)) {
					return false;
				}
			}
			return true;
		} else if (o instanceof Account) {
			return true;
		} else if (o instanceof Device) {
			Device d = (Device) o;
			if (n != null) {
				if (n) {
					if (d.getId() != null) {
						return false;
					}
				} else {
					if (d.getId() == null) {
						return false;
					}
				}
			}
			if (d.getId() != null) {
				Device e = trackerEngineDao.getDeviceById(d.getId());
				if (e == null) {
					return false;
				}
				if (!e.getCode1().equals(d.getCode1())) {
					return false;
				}
				if (!e.getCode2().equals(d.getCode2())) {
					return false;
				}
			}
			return true;
		} else if (o instanceof Location) {
			Location l = (Location) o;
			if (n != null) {
				if (n) {
					if (l.getId() != null) {
						return false;
					}
				} else {
					if (l.getId() == null) {
						return false;
					}
				}
			}
			if (l.getLatitude() == null) {
				return false;
			}
			if (l.getLongitude() == null) {
				return false;
			}
			if (l.getProvider() == null) {
				return false;
			}
			if (l.getRun() == null) {
				return false;
			}
			if (l.getTime() == null) {
				return false;
			}
			return true;
		} else if (o instanceof User) {
			return true;
		} else {
			return false;
		}
	}

	public void setTrackerEngineDao(TrackerEngineDao trackerEngineDao) {
		this.trackerEngineDao = trackerEngineDao;
	}
}
